package com.xncoding.pos.socket;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class TCPServer {
    public static void main(String[] args) {
        try {
            System.out.println("====我是服务端===");
            //1.自己创建一个服务ServerSocket  提供一个端口号
            ServerSocket server = new ServerSocket(9999);
            //2.等待客户端访问
            Socket socket = server.accept();
            //socket创建出来，证明三次握手连接成功了
            //字节形式的输出流
            OutputStream os = socket.getOutputStream();
            //将字节流包装成字符流，为了发送我们的文字提供方便
            PrintWriter writer = new PrintWriter(os);
            System.out.println("连接成功，输入你想发送的话把：");
            Scanner input = new Scanner(System.in);
            String str = input.nextLine();
            writer.println(str);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
